<?php

namespace Home\Controller;
use Think\Controller;

class PublicController extends  Controller {

    public function msg()
    {
        header("Content-type: text/html; charset=utf-8");
        $name = ( isset( $_SERVER['HTTP_X_FILENAME'] ) ? $_SERVER['HTTP_X_FILENAME'] : 0 );
        $title = ( isset( $_POST['title'] ) ? $_POST['title'] : '' );
        $contents = ( isset( $_POST['contents'] ) ? $_POST['contents'] : '' );

        if( $name ) {
            $title = ( isset( $_SERVER['HTTP_TITLE'] ) ? $_SERVER['HTTP_TITLE'] : '' );
            $contents = ( isset( $_SERVER['HTTP_CONTENTS'] ) ? $_SERVER['HTTP_CONTENTS'] : '' );

            $save_path = "./Public/uploads/files/";
            if( !is_dir( $save_path ) ) @mkdir( $save_path, 0777 );
            // 获取图片上传目录完整相对路径
            $imgUrl = date("Ymd")."/";
            // 获取图片上传目录完整物理路径
            $imgPath = $save_path.$imgUrl;
            //创建文件夹
            if( !is_dir( $imgPath ) ) @mkdir( $imgPath, 0777 );
            $fileName = date("YmdHis").'_'.rand(10000,99999 ).".".substr($name,-3,3);
            set_time_limit ( 0 );
            //生成文件
            if(file_put_contents( $imgPath.$fileName, file_get_contents('php://input') )){
                    $this->ajaxReturn($this->save($title,$contents,$imgPath.$fileName));
            }   // 1 or 0
           $this->ajaxReturn(0);
        }

        if( $title && $contents )
        {
            $this->ajaxReturn($this->save($title,$contents));
        }
        $this->ajaxReturn(0);

    }
    public function save($title,$contents,$imgPath=NULL){
            $message=M("messages");
            $message->startTrans();
            $datas['title']=$title;
            $datas['contents']=$contents;
            $datas['type'] = 2;
            $arlong['Eland'] = 1;
            $arlong['Bdata'] = 2;
            $arlong['Xcase'] = 3;
            $datas['module'] =  $arlong[MODULE_NAME] ;
            $datas['type']=$datas['type'];
            $datas['uid'] = $_SESSION['userInfo']['id'];
            $datas['regtime'] = time();
            $datas['file_url'] = $imgPath;
            $mid = $message->add($datas);
           if( $mid !==false){
                 $userm = M("UserMessages");
                 $udata['uid'] = $_SESSION['userInfo']['id'];
                 $udata['mid'] = $mid;
                 $udata['time'] = time();
                 if( $userm->add($udata) !== false ){
                     $message->commit();
                     return 1;
                 }
                  $message->rollback();
                  return 0;
           }
           $message->rollback();
           return 0;
    }
public function getMessage(){
        $id = I("get.id");
        $data['uid'] = $_SESSION['userInfo']['id'];
        $data['mid'] = $id;
        $data['time'] = time();
        $message =M("UserMessages");
        if($message->where(array('mid'=>$id,'uid'=>$_SESSION['userInfo']['id']))->count() == 0){
              $message->add($data);
        }
        $mes = M('Messages')->find($id);
        
        $mess = htmlspecialchars_decode(html_entity_decode( $mes['contents'] ,ENT_QUOTES));
        
        $mess = str_replace("{{name}}",$_SESSION['userInfo']['realname'],$mess);
        $this->ajaxReturn($mess);
    }
	
	public  function Exict(){
        session_unset($_SESSION['userInfo']);
        redirect("http://login.bdata.me");
    }
    
    /*
     * 提取关联关系
     * return =array('main'=>'区域，组团，开发商，楼盘名称','secondary'=>'业态，细分业态')
     */
    public function getrelation(){
        
        $data=F('Bdata/relation');
        $obj=M();
        if(empty($data)){
            $data['main']=$obj->query('select b.id as qid,qy,c.id as zid,zt,d.id as kid,a.kfs,e.id as pid,a.lpmc from mt_gx as a left join mt_region as b on a.qy=b.title left join mt_plate as  c on a.zt=c.title left join mt_company as d on a.kfs=d.title left join mt_project as e on a.lpmc=e.title');
           // $data['secondary']=
            
            $formats=$obj->query('select fid,b.title as ftitle,sfid,c.title as stitle from mt_formats_subformats as a inner join mt_formats as b on a.fid=b.id inner join mt_sub_formats as c on c.id=a.sfid');
            foreach($formats as $val){
                if(!isset($data['secondary'][$val['fid']])){
                    $data['secondary'][$val['fid']]=array(
                        'id'=>$val['fid'],
                        'name'=>$val['ftitle']
                    );
                }
                $data['secondary'][$val['fid']]['item'][]=array(
                    'id'=>$val['sfid'],
                    'name'=>$val['stitle']
                ); 
            }
            F('Bdata/relation',$data);
        }

        echo json_encode($data);
    }    

}